<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once '../php_class/DBManager.php';
require_once '../php_class/Security.php';
session_start();

$id = $_SESSION['name'];
$mail = htmlspecialchars($_POST['mail']);
$c_pass = htmlspecialchars($_POST['c_pass']);
$n_pass = htmlspecialchars($_POST['n_pass']);
$n_pass_r = htmlspecialchars($_POST['n_pass_r']);

DBManager::init();
$db = DBManager::$db;
$stt = $db->prepare('SELECT password, salt FROM user WHERE account_id = :name');
$stt->bindValue(':name', $id);
$stt->execute();
$items = $stt->fetch();

$sec = new Security();
$sec_pass = $sec->salt_pass($c_pass, $items['salt']);

if ($sec_pass === $items['password']) {
	$new_pass = null;
	if ($n_pass === $n_pass_r) {
		$new_pass = $sec->salt_pass($n_pass, $items['salt']);
		$mail = $sec->salt_pass($mail, $items['salt']);
	} else {
		header('Location: http://localhost/SocialAlbum/html/setting.html?pm=2');
		return;
	}
//	DBManager::init();
	$db = new DBManager();
	$db->updataUser($id, $mail, $new_pass);
	header('Location: http://localhost/SocialAlbum/html/setting.html?pm=0');
}
else{
	header('Location: http://localhost/SocialAlbum/html/setting.html?pm=1');
}

?>
